﻿using CRM.Extensions;
using CRM.Models;
using CRM.Services;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http;
using Microsoft.Extensions.Logging;
using CRM.ViewModels.customsData;
using Microsoft.EntityFrameworkCore;

namespace CRM.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CustomsDataController : BaseController
    {
        private readonly MyDbContext _dbContext;
        private readonly CustomsDataService _service;
        private readonly IHttpClientFactory _httpClientFactory;
        private readonly ILogger<CustomsDataController> _logger;

        public CustomsDataController(MyDbContext dbContext, CustomsDataService service, IHttpClientFactory httpClientFactory, ILogger<CustomsDataController> logger)
        {
            _dbContext = dbContext;
            _service = service;
            _httpClientFactory = httpClientFactory;
            _logger = logger;
        }

        //海关数据表的批量导入
        [HttpPost(nameof(uploadExcelTradeMode))]
        public async Task<ResultStruct> uploadExcelTradeMode(string tableName, List<TradeMode> dt)
        {
            try
            {
                int count = dt.Count();
                await _service.Add<TradeMode>(dt);
                //检查数据格式              
                return Success(count);
            }
            catch (Exception ex)
            {
                return Error(null, ex.Message, null);
            }
        }

        //海关数据表的批量导入
        [HttpPost(nameof(uploadExcelCommodity))]
        public async Task<ResultStruct> uploadExcelCommodity(string tableName, List<Commodity> dt)
        {
            try
            {
                int count = dt.Count();
                await _service.Add<Commodity>(dt);
                //检查数据格式              
                return Success(count);
            }
            catch (Exception ex)
            {
                return Error(null, ex.Message, null);
            }
        }
        //海关数据表的批量导入
        [HttpPost(nameof(uploadExcelTradePartner))]
        public async Task<ResultStruct> uploadExcelTradePartner(string tableName, List<TradePartner> dt)
        {
            try
            {
                int count = dt.Count();
                await _service.Add<TradePartner>(dt);
                //检查数据格式              
                return Success(count);
            }
            catch (Exception ex)
            {
                return Error(null, ex.Message, null);
            }
        }
        //海关数据表的批量导入
        [HttpPost(nameof(uploadExcelProvince))]
        public async Task<ResultStruct> uploadExcelProvince(string tableName, List<Province> dt)
        {
            try
            {
                int count = dt.Count();
                await _service.Add<Province>(dt);
                //检查数据格式              
                return Success(count);
            }
            catch (Exception ex)
            {
                return Error(null, ex.Message, null);
            }
        }
        //海关数据表的批量导入
        [HttpPost(nameof(uploadExcelExportCommodity))]
        public async Task<ResultStruct> uploadExcelExportCommodity(string tableName, List<ExportCommodity> dt)
        {
            try
            {
                int count = dt.Count();
                await _service.Add<ExportCommodity>(dt);
                //检查数据格式              
                return Success(count);
            }
            catch (Exception ex)
            {
                return Error(null, ex.Message, null);
            }
        }
        //海关数据表的批量导入
        [HttpPost(nameof(uploadExcelImportCommodity))]
        public async Task<ResultStruct> uploadExcelImportCommodity(string tableName, List<ImportCommodity> dt)
        {
            try
            {
                int count = dt.Count();
                await _service.Add<ImportCommodity>(dt);
                //检查数据格式              
                return Success(count);
            }
            catch (Exception ex)
            {
                return Error(null, ex.Message, null);
            }
        }
        //海关数据表的批量导入
        [HttpPost(nameof(uploadExcelUOMAdjustment))]
        public async Task<ResultStruct> uploadExcelUOMAdjustment(string tableName, List<UOMAdjust> dt)
        {
            try
            {
                int count = dt.Count();
                await _service.Add<UOMAdjust>(dt);
                //检查数据格式              
                return Success(count);
            }
            catch (Exception ex)
            {
                return Error(null, ex.Message, null);
            }
        }
        //海关数据表的批量导入
        [HttpPost(nameof(uploadExcelTradeData))]
        public async Task<ResultStruct> uploadExcelTradeData(string tableName, List<TradeData> dt)
        {
            try
            {
                int count = dt.Count();
                await _service.Add<TradeData>(dt);
                //检查数据格式              
                return Success(count);
            }
            catch (Exception ex)
            {
                return Error(null, ex.Message, null);
            }
        }

        //海关数据表查询方法
        [HttpGet(nameof(searchCustomsData))]
        public async Task<ResultStruct> searchCustomsData([FromQuery] SearchQuery listQuery)
        {
            IQueryable<TradeData> tradeDatas = _dbContext.Tradedata;
            //请输入查询条件    
            if (!string.IsNullOrEmpty(listQuery.CommodityId))
            {
                tradeDatas = tradeDatas.Where(c => c.Commodity.CommodityId == listQuery.CommodityId);
            }
            if (!string.IsNullOrEmpty(listQuery.TrademodeId) && listQuery.TrademodeId != "0")
            {
                tradeDatas = tradeDatas.Where(a => a.TradeMode.TrademodeId == Convert.ToInt32(listQuery.TrademodeId));
            }
            if (!string.IsNullOrEmpty(listQuery.TradepartnerId) && listQuery.TradepartnerId != "0")
            {
                tradeDatas = tradeDatas.Where(a => a.TradePartner.TradepartnerId == Convert.ToInt32(listQuery.TradepartnerId));
            }
            if (!string.IsNullOrEmpty(listQuery.ProvinceId) && listQuery.ProvinceId != "0")
            {
                tradeDatas = tradeDatas.Where(a => a.Province.ProvinceId == Convert.ToInt32(listQuery.ProvinceId));
            }
            var data = tradeDatas.Select(s => new
            {
                s.Id,
                s.CommodityId,
                s.Commodity.Commodityname,
                s.TradePartner.Tradepartnername,
                s.TradeMode.Trademodename,
                s.Province.Provincename,
                s.Firstquantity,
                s.Commodity.Firstuomname,
                s.Secondquantity,
                s.Commodity.Seconduomcode,
                s.Amountrmb
            });
            var result = await data.ToPaginateAsync(Request);
            return Success(result);
        }

        [HttpGet(nameof(searchTradepar))]
        public async Task<ResultStruct> searchTradepar()
        {
            var data = await _dbContext.Tradepartner.Select(s => new
            {
                s.TradepartnerId,
                s.Tradepartnername
            }).OrderBy(o => o.TradepartnerId).ToArrayAsync();
            return Success(data);
        }

        [HttpGet(nameof(searchTrademode))]
        public async Task<ResultStruct> searchTrademode()
        {
            var data = await _dbContext.Trademode.Select(s => new
            {
                s.TrademodeId,
                s.Trademodename
            }).OrderBy(o => o.TrademodeId).ToArrayAsync();
            return Success(data);
        }

        [HttpGet(nameof(searchProvince))]
        public async Task<ResultStruct> searchProvince()
        {
            var data = await _dbContext.Province.Select(s => new
            {
                s.ProvinceId,
                s.Provincename
            }).OrderBy(o => o.ProvinceId).ToArrayAsync();
            return Success(data);
        }
    }
}

